package com.ketai.service.impl;

import com.ketai.dao.CUserDao;
import com.ketai.dao.impl.CUserDaoImpl;
import com.ketai.pojo.CUser;
import com.ketai.query.ActivitesQuery;
import com.ketai.service.CUserService;
import com.ketai.util.Pages;
import com.ketai.vo.ActivityVo;
import com.ketai.vo.CUserVo;

import javax.naming.NamingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:胡远博
 * @Description:
 * @Date:Create In 2022-08-08 9:28
 **/
public class CUserServiceImpl implements CUserService {
    CUserDao cUserDao = new CUserDaoImpl();
    Pages<CUser> result = new Pages<CUser>();

    @Override
    public Pages<CUser> getAllCUser(Integer currentPage, Integer pageNumber) {
        List params = new ArrayList();

        StringBuilder sql = new StringBuilder("select * from c_user  ");
        StringBuilder sql2 = new StringBuilder("select count(*) total from c_user  ");
        try {
            long total = cUserDao.getTotalCUserCount(sql2.toString(), params.toArray());
            result.setDataTotal(total);
            Integer pageSize = pageNumber;
            result.setPageNumber(pageSize);
            Integer pageNo = currentPage;
            // 获取总页数  计算出总页数
            int pages = (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
            result.setTotalPages(pages);
            // 处理当前页码的问题   如果传过来的当前页大于总页数  赋值为总页数
            pageNo = pageNo > pages ? pages : pageNo;
            // 如果当前页小于第一页 赋值为第一页
            pageNo = pageNo <= 0 ? 1 : pageNo;
            //设置当前页面
            result.setCurrentPage(pageNo);
            // 分页查询
            sql = sql.append("  limit ?,? ");
            params.add((pageNo - 1) * pageSize);
            params.add(pageSize);
            List<CUser> cUserVoList = cUserDao.findAll(sql.toString(), params.toArray());
            result.setDataItems(cUserVoList);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return result;
    }


}
